package org.xfy.sys.controller;

import org.xfy.sys.domain.dto.RoleDto;
import org.xfy.sys.service.IRoleService;
import org.xfy.sys.domain.Role;
import org.xfy.sys.query.RoleQuery;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    public IRoleService roleService;

    /**
     * 获取菜单回显
     * @param roleId
     * @return
     */
    @GetMapping("/getMenu/{roleId}")
    public AjaxResult getMenu(@PathVariable("roleId") Long roleId){
        return roleService.getMenu(roleId);
    }

    /**
     * 获取权限回显
     * @param roleId
     * @return
     */
    @GetMapping("/permission/{roleId}")
    public AjaxResult getPermission(@PathVariable("roleId") Long roleId){
        return roleService.getPermissionByroleId(roleId);
    }
    /**
     * 接口：添加或修改
     *
     * @param role 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody RoleDto role) {
        if (role.getId() != null) {
            roleService.update(role);
        } else {
            roleService.add(role);
        }
        return AjaxResult.me();

    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        roleService.del(id);
        return new AjaxResult();
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        roleService.patchDel(ids);
        return new AjaxResult();
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Role findOne(@PathVariable("id") Long id) {
        return roleService.findOne(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<Role> findAll() {
        return roleService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/pageList")
    public PageList<Role> queryPage(@RequestBody RoleQuery query) {
        return roleService.queryPage(query);
    }
}
